﻿using System;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XWeb.Web.Sass;

namespace LibSaas.Test
{
	[TestClass]
	public class StringCompileTests
	{
		public readonly System.Text.RegularExpressions.Regex Spaces = new System.Text.RegularExpressions.Regex("\\s+");

		protected void Test(string source, string expectedResult)
		{
			var currentDirectory = Directory.GetCurrentDirectory();

			Trace.WriteLine("Current directory: " + currentDirectory);

			var result = SassCompiler.CompileString(source, currentDirectory);

			Assert.IsNotNull(result);
			
			if (result.Equals(expectedResult, StringComparison.OrdinalIgnoreCase))
				return;

			var normResult = result.Trim();
			var normExpectedResult = expectedResult.Trim();

			if (normResult.Equals(normExpectedResult, StringComparison.OrdinalIgnoreCase))
				return;

			normResult = Spaces.Replace(normResult, " ");
			expectedResult = Spaces.Replace(normExpectedResult, " ");

			if (normResult.Equals(normExpectedResult, StringComparison.OrdinalIgnoreCase))
				return;

			Trace.WriteLine("Expected: " + expectedResult);
			Trace.WriteLine("Actual: " + result);

			Assert.Fail();
		}

		[TestMethod]
		public void Simple1()
		{
			Test("body { margin: 0; }", "body { margin: 0; }");
			Test("p { display: none; }", "p { display: none; }");
			Test("a { text-decoration: none; } strong { font-weight: bold; }", "a { text-decoration: none; } strong { font-weight: bold; }");
		}

		[TestMethod]
		public void SimpleExpression()
		{
			Test("$a: 10em; $b: 20em; $c: $a*5-$b; div { width: $c / 10; }", "div { width: 3em; }");
		}

		[TestMethod]
		public void Nested()
		{
			Test("hgroup { h1, h2 { margin: 0; } &:hover { color: red; } }", "hgroup h1, hgroup h2 { margin: 0; } hgroup:hover { color: red; }");
		}

		[TestMethod]
		public void Import()
		{
			Test(string.Format("@import \"{0}\";", "file.scss"), "body html a, body html p { margin: 30em; }");
		}

		[TestMethod]
		public void Unicode()
		{
			const string str = "div:after { content: \"йцуфывячс\"; }";
			Test(str, str);
		}

		[TestMethod]
		public void UnicodeExpression()
		{
			const string str = "фыва";
			Test(string.Format("$a: \"{0}\"; $b: \"{0}\"; div:after {{ content: $a + $b; }}", str), string.Format("div:after {{ content: \"{0}{0}\"; }}", str));
		}
	}
}
